package contacts.view;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import contacts.controller.ContactsControllerListener;
import contacts.po.Student;

public class ContactsFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener controllerListener;
	private JScrollPane tablePanel;
	// JComboBox<String> comBox;
	public static int isbn = -1;// 选中的表格行索引
	public ContactsFrame() {
		this.controllerListener = new ContactsControllerListener();
		this.controllerListener.setContactsFrame(this); // 向controller注入frame，用于更新view。
		this.setJMenuBar(getContactsMenuBar());
		this.add(this.getContactsModifyToolBar(), BorderLayout.NORTH);
		this.add(this.getContactsTablePanel(controllerListener.getContactsService().findAllStudent()),
				BorderLayout.CENTER);
		this.setSize(1700, 450);
		this.setResizable(false);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.validate();
	}

//	private void getStudentPanel() { 
//		this.setLayout(new FlowLayout());
//		comBox = new JComboBox<String>();
//		JToolBar bar = new JToolBar();
//		bar.setFloatable(false);
//		comBox.addItem("泰山区9—417");
//		comBox.addItem("泰山区9—416");
//		bar.setAlignmentX(LEFT_ALIGNMENT);
//		JButton sureButton = new JButton("确定");
//		bar.add(sureButton);
//		sureButton.addActionListener(controllerListener);
//		sureButton.setActionCommand("surePost");
//		this.add(comBox);
//		this.add(bar);
//		if (comBox.getSelectedIndex() == 0) {
//			JTextField roomTextField;
//			roomTextField = new JTextField("泰山区9-416", 20);
//		}
//	}
	private JToolBar getContactsModifyToolBar() {
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(LEFT_ALIGNMENT);
		JButton addStudentButton = new JButton("添加");
		ImageIcon addIcon = new ImageIcon(ContactsFrame.class.getResource("/studentAdd.jpg"));
		addStudentButton.setIcon(addIcon);
		JButton searchStudentButton = new JButton("查询");
		ImageIcon searchIcon = new ImageIcon(ContactsFrame.class.getResource("/studentSearch.jpg"));
		searchStudentButton.setIcon(searchIcon);
		JButton updateStudentButton = new JButton("修改");
		ImageIcon updateIcon = new ImageIcon(ContactsFrame.class.getResource("/studentUpdate.jpg"));
		updateStudentButton.setIcon(updateIcon);
		JButton deleteStudentButton = new JButton("删除");
		ImageIcon deleteIcon = new ImageIcon(ContactsFrame.class.getResource("/studentDelete.jpg"));
		deleteStudentButton.setIcon(deleteIcon);
		addStudentButton.addActionListener(controllerListener);
		searchStudentButton.addActionListener(controllerListener);
		updateStudentButton.addActionListener(controllerListener);
		deleteStudentButton.addActionListener(controllerListener);
		addStudentButton.setActionCommand("addStudentDialog");
		searchStudentButton.setActionCommand("searchStudentDialog");
		updateStudentButton.setActionCommand("updateStudentDialog");
		deleteStudentButton.setActionCommand("deleteStudent");
		toolBar.add(addStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(searchStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(updateStudentButton);
		toolBar.add(new JToolBar.Separator());
		toolBar.add(deleteStudentButton);
		return toolBar;
	}

	public void refresh(List<Student> studentList) {
		System.out.println("测试——当前联系人数量为：" + studentList.size());
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(controllerListener.getContactsService().findAllStudent()),
				BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}

	/**
	 * 查询结果
	 */
	public void showSearchResult(List<Student> result) {
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}

	/**
	 * 菜单栏
	 * 
	 * @return
	 */
	private JMenuBar getContactsMenuBar() {
		JMenuItem addRoomMenuItem = new JMenuItem("添加宿舍");
		JMenuItem listStudentMenuItem = new JMenuItem("联系人列表");
		addRoomMenuItem.addActionListener(controllerListener);
		addRoomMenuItem.setActionCommand("addRoomDialog");
		listStudentMenuItem.addActionListener(controllerListener);
		listStudentMenuItem.setActionCommand("refreshContactsFrame");
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("联系人管理");
		fileMenu.add(addRoomMenuItem);
		fileMenu.add(listStudentMenuItem);
		bar.add(fileMenu);
		return bar;
	}

	private JScrollPane getContactsTablePanel(List<Student> studentList) {
		// tablePanel = new JPanel();
		String titles[] = { "编号", "学号", "姓名", "生日", "电话", "电子邮件", "籍贯", "家庭地址", "宿舍号", "宿舍名称" };
		Object[][] data = new Object[studentList.size()][10];
		Iterator<Student> iter = studentList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Student student = iter.next();
			data[i][0] = student.getStudentId();
			data[i][1] = student.getStudentNo();
			data[i][2] = student.getStudentName();
			data[i][3] = student.getBirthday();
			data[i][4] = student.getTelephone();
			data[i][5] = student.getEmail();
			data[i][6] = student.getBirthPlace();
			data[i][7] = student.getAddress();
			data[i][8] = student.getRoom().getRoomNo();
			data[i][9] = student.getRoom().getRoomName();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		// tablePanel.add(new JScrollPane(table));
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				int studentId = Integer.parseInt(table.getValueAt(isbn, 0).toString().trim());
				String studentNo = table.getValueAt(isbn, 1).toString().trim();
				String studentName = table.getValueAt(isbn, 2).toString().trim();
				System.out.println("Isbn=" + isbn + ",studentId=" + studentId + ",studentNo=" + studentNo
						+ ",studentName=" + studentName);
				FunctionFrame functionFrame = new FunctionFrame(); // 修改生日的界面
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}

	public ContactsControllerListener getControllerListener() {
		return controllerListener;
	}

	public int getIsbn() {
		return isbn;
	}

}
